/**
* Oxide
*
* Copyright (c) 2011 Dinosaur Kick Pty Ltd, All rights reserved.
*
* This source code is made available under the "Modified BSD" licensing arrangement.
* See license.txt for details.
*/
// ----------------------------------------------------------------------------
#include <iterator>
#include <vector>
#include <fstream>
#include <boost/foreach.hpp>
#include <boost/progress.hpp>
#include "oxide/process.hpp"
#include "oxide-common/renderer/oxide_renderer.hpp"
#include "oxide-codegen-php/php_renderer.hpp"

using namespace o2;
using namespace o2::lex;
using namespace o2::parse;
using namespace o2::codegen;

std::vector<wtoken> tokenize(std::wifstream& stream)
{
	typedef std::istream_iterator<wchar_t, wchar_t> iterator;
	o2::lex::basic_lexer<iterator> lexer(iterator(stream), (iterator()));
	std::vector<wtoken> tokens(lexer.begin(), lexer.end());
	return tokens;
}

wresult parse_tokens(const std::vector<wtoken>& tokens)
{
	basic_parser<std::vector<wtoken>::const_iterator> parser(tokens.begin(), tokens.end());
	return parser.parse();
}

int main(int argc, char* argv[]) {

	if (argc != 2)
	{
		std::cout << "Filename must be provided." << std::endl;
		return 1;
	}
	
	std::wifstream stream(argv[1]);
	stream.unsetf(std::ios::skipws);
	std::vector<wtoken> tokens = tokenize(stream);
	
	BOOST_FOREACH(const wtoken& t, tokens)
	{
		std::wcout << t << std::endl;
	}
	
	std::wcout << std::endl;
	
	wresult res = parse_tokens(tokens);
	
	BOOST_FOREACH(const wmessage& msg, res.messages())
	{
		std::wcout << '[' << msg.line << L"] " << msg.text << std::endl;
	}
	
	renderer::woxide_renderer render;
	std::wcout << render(*res.ast()) << std::endl;
	
	std::wcout << std::endl;
	
	php::wphp_renderer php;
	std::wcout << php(*res.ast()) << std::endl;
	
	return 0;
}
